package com.chj.lintcode.backpack;

public class Code02_backpackII {
	public static int backPackII(int m, int[] A, int[] V) {
		// write your code here
		int n = A.length;
		if (n == 0 || m == 0) {
			return 0;
		}
		int[][] dp = new int[n + 1][m + 1];
		dp[0][0] = 0;
		for (int i = 1; i <= m; i++) {
			dp[0][i] = 0;
		}

		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= m; j++) {
				dp[i][j] = dp[i - 1][j];
				if (j >= A[i - 1]) {
					dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - A[i - 1]] + V[i - 1]);
				}
			}
		}

		return dp[n][m];
	}

	public static void main(String[] args) {
		{
			int[] nums = { 2, 3, 5, 7 };
			int[] vs = { 1, 5, 2, 4 };
			int target = 10;
			System.out.println(backPackII(target, nums, vs));
		}

		{
			int[] nums = { 2, 3, 8 };
			int[] vs = { 2, 5, 8 };
			int target = 10;
			System.out.println(backPackII(target, nums, vs));
		}
	}
}
